package com.sumansoul.multiplerecycleview;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import brvh.ExamCourseAdapter;
import brvh.ExamCourseSection;
import brvh.ExamInfoBean;
import brvh.MultipleItem;

/**
 * BRVH分组
 */
public class BRVHGroupingActivity extends Activity implements BaseQuickAdapter.OnItemClickListener {
    private RecyclerView recyclerView;
    private ExamCourseAdapter examCourseAdapter;
    private List<ExamInfoBean.ExamDataBean> beanList = new ArrayList<>();
    private View noData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grouping);
        recyclerView = findViewById(R.id.recyclerView);
        initData();
        initView();
        setListData();
    }

    private void initData() {
        for (int i = 0; i < 3; i++) {
            ExamInfoBean.ExamDataBean dataBean = new ExamInfoBean.ExamDataBean();
            dataBean.setTitle("标题" + i);
            List<ExamInfoBean.ExamDataBean.InfoListBean> list = new ArrayList<>();
            for (int j = 0; j < 10; j++) {
                ExamInfoBean.ExamDataBean.InfoListBean listBean = new ExamInfoBean.ExamDataBean.InfoListBean();
                listBean.setTitle("内容" + j);
                list.add(listBean);
                dataBean.setInfoList(list);
            }
            beanList.add(dataBean);
        }
    }

    private void initView() {
        //noData = LayoutInflater.from(this).inflate(R.layout.recyclerview_empty_view3, (ViewGroup) recyclerView.getParent(), false);
        examCourseAdapter = new ExamCourseAdapter(R.layout.item_content, R.layout.item_header, new ArrayList<ExamCourseSection>());
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.setAdapter(examCourseAdapter);
        examCourseAdapter.setOnItemClickListener(this);
    }

    public void setListData() {
        if (beanList == null || beanList.size() == 0) {
            examCourseAdapter.setEmptyView(noData);
            examCourseAdapter.setNewData(new ArrayList<ExamCourseSection>());
        } else {
            examCourseAdapter.setNewData(initList());
        }
    }

    private List<ExamCourseSection> initList() {
        List<ExamCourseSection> list = new ArrayList<>();
        if (beanList.size() == 0) {
            return list;
        }
        for (int i = 0; i < beanList.size(); i++) {
            ExamCourseSection sectionHeader = new ExamCourseSection(true, beanList.get(i).getTitle());
            list.add(sectionHeader);
            if (beanList.get(i).getInfoList().size() != 0) {
                for (int j = 0; j < beanList.get(i).getInfoList().size(); j++) {
                    ExamCourseSection section = new ExamCourseSection(beanList.get(i).getInfoList().get(j));
                    list.add(section);
                }
            }
        }
        return list;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        ExamInfoBean.ExamDataBean.InfoListBean bean = ((ExamCourseSection) adapter.getItem(position)).t;
        Toast.makeText(BRVHGroupingActivity.this, "选择了" + bean.getTitle(), Toast.LENGTH_SHORT).show();
    }
}
